/******************************************************************************
 * Copyright 2023 The Apollo Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include "modules/canbus_vehicle/x3epro/protocol/scu_rearwheelspeed_302.h"
#include "glog/logging.h"
#include "modules/drivers/canbus/common/byte.h"
#include "modules/drivers/canbus/common/canbus_consts.h"

namespace apollo {
namespace canbus {
namespace x3epro {

using ::apollo::drivers::canbus::Byte;

Scurearwheelspeed302::Scurearwheelspeed302() {}
const int32_t Scurearwheelspeed302::ID = 0x302;

void Scurearwheelspeed302::Parse(const std::uint8_t* bytes, int32_t length,
                         X3epro* chassis) const {
  chassis->mutable_scu_rearwheelspeed_302()->set_failurerank(failurerank(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_vehiclespeed_m(vehiclespeed_m(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rl_valid(wheelspeed_rl_valid(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rl(wheelspeed_rl(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rr_valid(wheelspeed_rr_valid(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rr(wheelspeed_rr(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_alivecounter_rear(alivecounter_rear(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rl_direct(wheelspeed_rl_direct(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_wheelspeed_rr_direct(wheelspeed_rr_direct(bytes, length));
  chassis->mutable_scu_rearwheelspeed_302()->set_checksum_rear(checksum_rear(bytes, length));
}

// config detail: {'bit': 7, 'enum': {0: 'FAILURERANK_NOMAL', 1: 'FAILURERANK_LEVEL_1', 2: 'FAILURERANK_LEVEL_2', 3: 'FAILURERANK_LEVEL_3', 4: 'FAILURERANK_LEVEL_4', 5: 'FAILURERANK_LEVEL_5', 6: 'FAILURERANK_RESERVED_6', 7: 'FAILURERANK_RESERVED_7'}, 'is_signed_var': False, 'len': 3, 'name': 'failurerank', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Scu_rearwheelspeed_302::FailurerankType Scurearwheelspeed302::failurerank(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 0);
  int32_t x = t0.get_byte(5, 3);

  Scu_rearwheelspeed_302::FailurerankType ret =  static_cast<Scu_rearwheelspeed_302::FailurerankType>(x);
  return ret;
}

// config detail: {'bit': 4, 'is_signed_var': False, 'len': 13, 'name': 'vehiclespeed_m', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'Km/h ->  m/s', 'precision': 0.05625, 'type': 'double'}
double Scurearwheelspeed302::vehiclespeed_m(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 0);
  int32_t x = t0.get_byte(0, 5);

  Byte t1(bytes + 1);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.056250 / 3.6;
  return ret;
}

// config detail: {'bit': 23, 'enum': {0: 'WHEELSPEED_RL_INVALID', 1: 'WHEELSPEED_RL_VALID'}, 'is_signed_var': False, 'len': 1, 'name': 'wheelspeed_rl_valid', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Scu_rearwheelspeed_302::Wheelspeed_rl_validType Scurearwheelspeed302::wheelspeed_rl_valid(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 2);
  int32_t x = t0.get_byte(7, 1);

  Scu_rearwheelspeed_302::Wheelspeed_rl_validType ret =  static_cast<Scu_rearwheelspeed_302::Wheelspeed_rl_validType>(x);
  return ret;
}

// config detail: {'bit': 22, 'is_signed_var': False, 'len': 15, 'name': 'wheelspeed_rl', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'Km/h ->  m/s', 'precision': 0.01, 'type': 'double'}
double Scurearwheelspeed302::wheelspeed_rl(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 2);
  int32_t x = t0.get_byte(0, 7);

  Byte t1(bytes + 3);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.010000 / 3.6;
  return ret;
}

// config detail: {'bit': 39, 'enum': {0: 'WHEELSPEED_RR_INVALID', 1: 'WHEELSPEED_RR_VALID'}, 'is_signed_var': False, 'len': 1, 'name': 'wheelspeed_rr_valid', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Scu_rearwheelspeed_302::Wheelspeed_rr_validType Scurearwheelspeed302::wheelspeed_rr_valid(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 4);
  int32_t x = t0.get_byte(7, 1);

  Scu_rearwheelspeed_302::Wheelspeed_rr_validType ret =  static_cast<Scu_rearwheelspeed_302::Wheelspeed_rr_validType>(x);
  return ret;
}

// config detail: {'bit': 38, 'is_signed_var': False, 'len': 15, 'name': 'wheelspeed_rr', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'Km/h ->  m/s', 'precision': 0.01, 'type': 'double'}
double Scurearwheelspeed302::wheelspeed_rr(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 4);
  int32_t x = t0.get_byte(0, 7);

  Byte t1(bytes + 5);
  int32_t t = t1.get_byte(0, 8);
  x <<= 8;
  x |= t;

  double ret = x * 0.010000 / 3.6;
  return ret;
}

// config detail: {'bit': 51, 'is_signed_var': False, 'len': 4, 'name': 'alivecounter_rear', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
int Scurearwheelspeed302::alivecounter_rear(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(0, 4);

  int ret = x;
  return ret;
}

// config detail: {'bit': 53, 'enum': {0: 'WHEELSPEED_RL_DIRECT_INVALID', 1: 'WHEELSPEED_RL_DIRECT_D', 2: 'WHEELSPEED_RL_DIRECT_N', 3: 'WHEELSPEED_RL_DIRECT_R'}, 'is_signed_var': False, 'len': 2, 'name': 'wheelspeed_rl_direct', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Wheelspeed_directType Scurearwheelspeed302::wheelspeed_rl_direct(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(4, 2);

  Wheelspeed_directType ret =  static_cast<Wheelspeed_directType>(x);
  return ret;
}

// config detail: {'bit': 55, 'enum': {0: 'WHEELSPEED_RR_DIRECT_INVALID', 1: 'WHEELSPEED_RR_DIRECT_D', 2: 'WHEELSPEED_RR_DIRECT_N', 3: 'WHEELSPEED_RR_DIRECT_R'}, 'is_signed_var': False, 'len': 2, 'name': 'wheelspeed_rr_direct', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': 'bit', 'precision': 1.0, 'type': 'enum'}
Wheelspeed_directType Scurearwheelspeed302::wheelspeed_rr_direct(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 6);
  int32_t x = t0.get_byte(6, 2);

  Wheelspeed_directType ret =  static_cast<Wheelspeed_directType>(x);
  return ret;
}

// config detail: {'bit': 63, 'is_signed_var': False, 'len': 8, 'name': 'checksum_rear', 'offset': 0.0, 'order': 'motorola', 'physical_range': '[0|0]', 'physical_unit': '', 'precision': 1.0, 'type': 'int'}
int Scurearwheelspeed302::checksum_rear(const std::uint8_t* bytes, int32_t length) const {
  Byte t0(bytes + 7);
  int32_t x = t0.get_byte(0, 8);

  int ret = x;
  return ret;
}
}  // namespace x3epro
}  // namespace canbus
}  // namespace apollo
